const { $Toast } = require('../../dist/base/index');
const server = require("../../utils/server.js");
var app = getApp();
Page({
  data: {
    phone: '',
    password: '',
    securityCode:'',
    password2:'',
    currentTime:60,
    time:"获取验证码",
    vCode:false,
    formStatus:false,
    disabled:true,
    checked:true,
    http:'注册账号即表示您同意遵守E棒棒用户协议跟隐私政策',
    UUID:"Q1W2E3R4T5Y6U7I8O9PASDFGHJKL"
  },
  onLoad: function () {
    let id = wx.getStorageSync("uid");
    if (id) {

      wx.switchTab({
        url: '../index/index',
      })
    } else {

    }
  },
  // 获取输入验证码
  codeInput: function (e) {
    
     this.setData({
      securityCode: e.detail.value
    })
  },
  // 获取输入账号 
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 获取输入密码 
  password2Input: function (e) {
    this.setData({
      password2: e.detail.value
    })
  },
 //协议
  change(){
    let that=this;
    that.setData({
      checked: !that.data.checked
    })
  },
  // 登录 
  submitRegist: function () {
    var self = this;
    if(!self.vertifyCode(this.data.securityCode)){
      this.handleText("验证码不正确");
      return false;
    }
    else if (!this.data.phone || !this.data.password || !this.data.password2 || !this.data.securityCode) {
    
      this.handleText("请填写完整");
      return false;

    }  else if (this.data.password2 != this.data.password){
      this.handleText("密码不一致");
      return false;
    }
    else{
      that.setData({
        formStatus: true,
      })
      // if (!self.formStatus){
      //   return false;
      // }
      server.postReq("/mobile/createUser", {
        "phone": self.data.phone,
        "passWord": self.data.password2
      }, function (res) {
        if (res.data.code == 1) {
               self.handleSuccess("注册成功！");
               setTimeout(function(){
                 wx.navigateBack({
                   url: '../login/login'
                 })
               },1000)
              
          // wx.setStorage({
          //   key: 'uid',
          //   data: res.data.data.id,
          //   success: function (res) {
         

          //   }
          // })
        }
      })

    }
 
  },
  vertifyCode(code){
    let that = this;
    let flag=false;
    if (!that.data.phone){
      return false;
    }
    server.postReq('/mobile/SelectVerify', {
      phone: that.data.phone,
      securityCode:that.data.securityCode
    }, function (res) {
       //that.handleError(res.data.msg);
      if (res.data.code=='1') {
        that.setData({
          formStatus: true,
        })
          flag=true;
       
      }else{
        that.setData({
          formStatus:false,
        })
         flag = false;
      }
    })
    return flag;
  },
  sendCode(){
    let that=this;
    var phoneReg=/^1[3-9]\d{9}$/;
    if (!phoneReg.test(this.data.phone)){
      that.handleWarning("手机号输入不正确");
      return false;
    }
    
    server.postReq('/mobile/toregister',{
      phone: that.data.phone,
      UUID:that.data.UUID
    },function(res){
      that.handleSuccess(res.data.msg)
      if(res.data.code==1){
        
      }
    })
  },
  getCode: function (options) {
 
    var that = this;
    var currentTime = that.data.currentTime;
    if (!that.data.phone) {
      that.handleWarning('请输入手机号');
      return false;
    };
    that.sendCode();
    that.setData({
      time: currentTime + '秒'
    })
   var  interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1) + '秒',
        disabled: false,
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: true
        })
      }
    }, 1000)
  },
 
  handleText(msg) {
    $Toast({
      content: msg
    });
  },
  handleSuccess(successMsg) {
    $Toast({
      content: successMsg,
      type: 'success'
    });
  },
  handleWarning(msg) {
    $Toast({
      content: msg,
      type: 'warning'
    });
  },
  handleError(msg) {
    $Toast({
      content: msg,
      type: 'error'
    });
  },
  handleLoading() {
    $Toast({
      content: '加载中',
      type: 'loading'
    });
  },
}) 